#!/bin/bash
set -e
if [ -z "${MountPoint}" ]; then
    EnvFile="../common/0_env.sh"
    # shellcheck source=../common/0_env.sh
    source ${EnvFile}
fi

echo -e "\n***********************************************************************"
echo -e "\t\t$0 : 测试orcafs-ctl --setquota 命令是否正常"
echo -e "***********************************************************************"

tmpDirPath="/test_setquota_tmp"
absTmpDirPath="${MountPoint}${tmpDirPath}"
SysUid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $3}'`
SysGid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $4}'`
setcmd="orcafs-ctl --setquota "

function test_arg_dir()
{
    echo -e "\n--------------------测试 --dir 参数--------------------------"
    echo -e "\n异常测试 --dir 必须指定且存在，不包含挂载点路径"
    test_error_cmd "orcafs-ctl --setquota"

    echo -e "\n异常测试 --dir 不能包含挂载点路径"
    test_error_cmd "${setcmd} --dir=${absTmpDirPath}"

    echo -e "\n异常测试 --dir 必须存在"
    test_error_cmd "${setcmd} --dir=${tmpDirPath}-1"

    echo -e "\n正常测试 --dir"
    test_cmd "${setcmd} --dir=${tmpDirPath}"
    remove_quota_dir ${tmpDirPath}
}

function test_arg_uid_gid()
{
    echo -e "\n--------------------测试 --uid/gid 参数--------------------------"
    echo -e "\n异常测试 --uid/gid 必须存在"
    test_error_cmd "${setcmd} --dir=${tmpDirPath} --uid=${SysUid}X"
    test_error_cmd "${setcmd} --dir=${tmpDirPath} --gid=${SysGid}X"

    echo -e "\n正常测试 --uid/gid"
    test_cmd "${setcmd} --dir=${tmpDirPath} --uid=${SysUid}"
    remove_quota_dir ${tmpDirPath} --uid="${SysUid}"
    test_cmd "${setcmd} --dir=${tmpDirPath} --gid=${SysGid}"
    remove_quota_dir ${tmpDirPath} --gid="${SysGid}"
}

function test_arg_soft_small_than_hard()
{
    echo -e "\n--------------------测试 --soft/hard 参数--------------------------"
    echo -e "\n异常测试 softsize 必须小于 hardsize"
    setSizeStr="--hardsize=1g --softsize=2g"
    test_error_cmd "${setcmd} ${setSizeStr} --dir=${tmpDirPath}"

    echo -e "\n异常测试 softsize 必须小于 hardsize"
    setNumStr="--hardnum=10000 --softnum=20000"
    test_error_cmd "${setcmd} ${setNumStr} --dir=${tmpDirPath}"

    echo -e "\n正常测试 soft 小于 hard"
    setSizeStr="--hardsize=2g --softsize=1g"
    setNumStr="--hardnum=20000 --softnum=10000"
    test_cmd "${setcmd} ${setSizeStr} ${setNumStr} --dir=${tmpDirPath}"
    remove_quota_dir ${tmpDirPath}
}

function test_arg_used_small_than_limit()
{
    echo -e "\n--------------------测试 usedsize/usednum 必须小于硬阈值和软阈值--------------------------"
    echo -e "\n异常测试 目录已有容量大于softsize/hardzise"
        dd if=/dev/zero of="${absTmpDirPath}/file1" bs=2M count=1
        dd if=/dev/zero of="${absTmpDirPath}/file2" bs=2M count=1

        setSizeStr="--hardsize=2m"
        test_error_cmd "${setcmd} ${setSizeStr} --dir=${tmpDirPath}"

        setSizeStr="--softsize=2m"
        test_error_cmd "${setcmd} ${setSizeStr} --dir=${tmpDirPath}"

    echo -e "\n异常测试 目录已有文件个数大于softnum/hardnum"
        setNumStr="--hardnum=2"
        test_error_cmd "${setcmd} ${setNumStr} --dir=${tmpDirPath}"

        setNumStr="--softnum=2"
        test_error_cmd "${setcmd} ${setNumStr} --dir=${tmpDirPath}"

    echo -e "\n正常测试 --force 不再检查目录已有容量是否小于softsize/hardzise"
        test_cmd "${setcmd} ${setSizeStr} ${setNumStr} --dir=${tmpDirPath} --force"

    remove_quota_dir ${tmpDirPath}
}

function test_arg_usednum_usedsize()
{
    echo -e "\n---------------测试 --usedsize/usednum 必须同时指定----------------"
    usedStr="--usedsize=100"
    test_error_cmd "${setcmd} ${usedStr} --dir=${tmpDirPath}"

    usedStr="--usednum=100"
    test_error_cmd "${setcmd} ${usedStr} --dir=${tmpDirPath}"

    usedStr="--usedsize=100 --usednum=100"
    test_cmd "${setcmd} ${usedStr} --dir=${tmpDirPath}"

    remove_quota_dir ${tmpDirPath}
}

rm_abs_dir "${absTmpDirPath}"
mkdir -p "${absTmpDirPath}"
orcafs-ctl --setpattern --storagepoolid=2 "${absTmpDirPath}" &> /dev/null

test_arg_dir
test_arg_uid_gid
test_arg_soft_small_than_hard
test_arg_used_small_than_limit
test_arg_usednum_usedsize

rm_abs_dir "${absTmpDirPath}"
